
use ../Intermediate/analysis_plant_plans, clear
bys TrackerID: gen cx_plant = _n == 1
keep if RetiredYr >= 2015 & RetiredYr <= 2022 & operating_2014 == 1 & gcpt_earl_annoucement__rtr_yr  == . & cx_plant == 1
keep TrackerID TrackerLOC Plant Unit RetiredYr Wikipage 
replace Plant = lower(Plant)
duplicates drop
tempfile plants 
save `plants'

//below data file corresponds to all the above plants wiki pages run through ChatGPT to extract retirement announcements
import excel using ../Raw/GEM/GEM-Wiki-ChatGPT-Extract-Announcements.xlsx, firstrow  clear
rename PlantName Plant
replace Plant = lower(Plant)

merge 1:1 Plant Unit using `plants', nogen keep(3)


rename AnnouncementDate AnnouncementDate_gem_wiki
rename PlannedRetirementDate PlannedRetirementDate_gem_wiki
rename ActualRetirementDate ActualRetirementDate_gem_wiki 

//get clean announcement year 
gen ann_year = word(AnnouncementDate_gem_wiki,-1)
replace ann_year = word(AnnouncementDate_gem_wiki,1) if ann_year == "(proposal)"
replace ann_year = ""  if inlist(AnnouncementDate_gem_wiki,"Not specified","Not Specified","Unknown")
destring ann_year, replace

//get clean plannt ret year 
gen plan_year = word(PlannedRetirementDate_gem_wiki,-1)
replace plan_year = ""  if inlist(PlannedRetirementDate_gem_wiki,"Not specified","Not Specified","Unknown")
replace plan_year = word(PlannedRetirementDate_gem_wiki,1) if regexm(plan_year,"(planned)") | regexm(plan_year,"conversion") 
replace plan_year = word(PlannedRetirementDate_gem_wiki,1) if regexm(plan_year,"mothballing")
destring plan_year, force replace
rename plan_year plan_ret_year_gem_wiki
rename ann_year announce_ret_year_gem_wiki

keep TrackerID   *_gem_wiki
order TrackerID
compress

save ../Intermediate/gem_wiki_announcement_match, replace
